package com.lilong.jwt.common.base;

import com.lilong.jwt.entity.user.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 用于提供Controll中用的公共方法
 */
public abstract class BaseController  {

    @Value("${jwt.header}")
    protected String tokenHeader;

    protected String getToken() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        return request.getHeader(tokenHeader);
    }
    protected User getOperator(){
        UsernamePasswordAuthenticationToken authentication=(UsernamePasswordAuthenticationToken)SecurityContextHolder.getContext().getAuthentication();
        return (User)authentication.getPrincipal();
    }
}
